<?php

/*
**	Restrict direct access
*/

defined("_dispatch")	or	die("Access Denied !");

/*
**	AnimusDispatcher	-	Singleton Router Class
*/

define('DOMROOT',dirname(dirname(dirname(__FILE__)).'/'));
		
class	AnimusDispatcher{

        protected $_urlMap;

        protected $_current_request;

        public function setCurrentRequest(AnimusRequest $Request){
            $this -> _current_request = $Request;
            return;
        }

        public function getCurrentRequest(){
            return $this -> _current_request;
        }

        public function lookup(){
                $this->_urlMap	=	new	SimpleXMLElement(	file_get_contents(	DOMROOT.'/config/routes.xml'	));
                $_urlMap	=	$this->_urlMap;
                foreach(	$_urlMap	->	children()	->	{'component-mapping'}	as	$_compMapping	){
                        $pattern = (string)$_compMapping -> {'url-pattern'};
                        $request = $this->_current_request->getHttpRequestURI();
                        if( preg_match( $pattern, $request))
                          $compName = (String) $_compMapping -> {'name'};
                }
                foreach( $_urlMap -> children() -> {'component'} as $_comp ){
                        if( strcmp ( $_comp -> name , $compName ) == 0)
                          $compPath = (String) $_comp -> path;
                }
                if(isset($compPath)){
                         $this ->_current_request->setComponentPath(DOMROOT.$compPath);	
                         $this ->_current_request->setComponent($compName);       
                        return;
                }
                else{   
                        $compPath = "/components/ErrorComponent.php";
                        $this ->_current_request->setComponentPath(DOMROOT.$compPath);	
                        $this ->_current_request->setComponent(ErrorComponent);       
                        return ;
                }
        }
}
?>